<template>
	<view>

		<form>
			<view class="cu-form-group margin-top">
				<view class="title">品牌</view>
				<picker @change="brandsChange" :value="bransindex" :range="brands">
					<view class="picker">
						{{brandName}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">型号</view>
				<input placeholder="XXXX款XX" v-model="xingHao" name="input"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">行驶里程</view>
				<input placeholder="" type="number" v-model="Wkm" name="input"></input> <view>万公里</view>
			</view>
			
			<view class="cu-form-group">
				<view class="title">上户日期</view>
				<picker mode="date" :value="regdate" start="2000-01-01" end="2025-01-01" @change="RegChange">
					<view class="picker">
							{{regdate}}
					</view>
				</picker>
			</view>
			
			<view class="cu-form-group margin-top">
				<view class="title">排量</view>
				<input type="number" name="input" v-model="engineCC"></input>
					  <view class="title"><text>{{TL}}</text> 涡轮增压</view>
						  <view>
							  <checkbox-group class="block" @change="hastChange">
									<checkbox  :checked="hasT"  />
							  </checkbox-group>
					      </view>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">变速箱</view>
				<picker @change="SpeedChange" :value="speedsindex" :range="speeds">
					<view class="picker">
						{{speedAssembly}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">交强险到期日</view>
			<picker mode="date" :value="jQXDaoQi" start="2018-01-01" end="2030-01-01" @change="JQXChange">
				<view class="picker">
						{{jQXDaoQi=='2020-01-01'?'已到期':jQXDaoQi}}
				</view>
			</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">商业险到期日</view>
			<picker mode="date" :value="sYxDaoQi" start="2018-01-01" end="2030-01-01" @change="SYXChange">
				<view class="picker">			 
						{{sYxDaoQi=='2020-01-01'?'已到期':sYxDaoQi}}
				</view>
			</picker>
			</view>
			
			<view class="cu-form-group">
				<view class="title">收车价</view>
				<input type="number"  name="input" v-model="costPrice"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">显示卖价</view>
				<input  type="number" name="input"  v-model="showPrice"></input>
			</view>
			<view class="cu-form-group">
				<view class="title" >内部可卖价</view>
				<input  type="number" name="input"  v-model="sellPrice"></input>
			</view>
			<view class="cu-form-group margin-top">
				<textarea maxlength="-1" :disabled="modalName!=null"  v-model="showTxt" placeholder="显示信息"></textarea>
			</view>
			
			<view class="cu-form-group margin-top">
				<textarea maxlength="-1" :disabled="modalName!=null"    v-model="hideTxt" placeholder="内部可见信息"></textarea>
			</view>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/9
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<10">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>		
			<view class="flex margin-top">
				<view class="cu-progress round">
				<progress :percent="percent" strock-width="25" ></progress>
				</view>
			</view>
			 <view class="padding flex flex-direction">
			 	<button class="cu-btn bg-red margin-tb-sm lg" @click="SubmitForm">提交</button>
			 </view>
		 <view >
			
		</view>
	
		
		</form>
	</view>
</template>

<script>
	 import service from '../../service.js';
	 var _self;
	export default {
		data() {
			return {
				id:0,
				brands:[],
				xingHao:'',
				engineCC:'',
				sellPrice : '',
				showPrice: '',
				costPrice: '',
				bransindex:-1,
				speeds:['手动','自动','CVT','双离合'],
				speedsindex:-1,
				sYxDaoQi: '2020-01-01',
				jQXDaoQi:'2020-01-01',
				regdate:'2015-01-01',
				imgList: [],
				upimgList: [],
				modalName: null,
				showTxt: '',
				hideTxt: '',
				hasT:true,
				percent:0,
				Wkm:'',
				speedAssembly:'',
				brandName:'',
				TL:''
			};
		},
		onLoad:function(e){
			_self=this; 
			this.inidata(e);
		},
		methods: { 
			inidata(e){
				_self.id=e.carid;
				service.referToken(); //刷新，并判断是否要登录
				//1.加载品牌
				 uni.request({
				 	url: service.BaseUrl+'api/apicar/GetBrand',
				 	method: 'GEt',
				 	success: res => {
				 			if (res.data.Code==1) {
				 				this.brands=res.data.Data;
				 			} 
				 	}
				 });
				 //初始化显示数据
				 uni.request({ 
				 	url: service.BaseUrl+'api/apiCar/GetCarByid',
				 	method: 'POST',
				 	data: {
				 		ID:e.carid,
				 		Token: service.getUsers().Token
				 	},
				 	success: res => {
				 		if(res.data.Code==1){
				 			var dt=res.data.Data;
				 			console.log(dt);
				 			_self.brandName=dt.brandName;
				 			_self.xingHao=dt.xingHao;
				 			_self.Carimgs=dt.Carimgs;
				 			_self.Wkm=dt.Wkm;
				 			_self.regdate=dt.regdate.substring(0,10);
							console.log('上户日期'+_self.regdate);
				 			_self.speedAssembly=dt.speedAssembly;
				 			_self.engineCC=dt.engineCC;
				 			var today =parseInt(new Date().getTime() / 1000);
				 			_self.jQXDaoQi =service.FormatDate(dt.jQXDaoQi);
				 			_self.sYxDaoQi =service.FormatDate(dt.sYxDaoQi);
				 			 _self.showTxt=dt.showTxt;
				 			_self.TL=dt.hasT?'T':'L';
				 			_self.hideTxt=dt.hideTxt;						
				 			_self.sellPrice=dt.sellPrice;
				 			_self.showPrice=dt.showPrice;
				 			_self.costPrice=dt.costPrice;
				 			_self.closePrice=dt.closePrice;
							_self.hasT=dt.hasT;
							_self.imgList=dt.Carimgs; 
				 		}
				 		else{
				 			uni.showToast({
				 				title:"查询失败"
				 			}) 
				 		}
				 	
				 	},
				 	fail: () => {},
				 	complete: () => {}
				 });
				 
				 
			},
			brandsChange(e) {
				this.bransindex = e.detail.value
				this.brandName=_self.brands[this.bransindex];
			}, 			
			SpeedChange(e) {
				this.speedsindex = e.detail.value
				this.speedAssembly=_self.speeds[this.speedsindex];
			}, 
			SYXChange(e) {
				this.sYxDaoQi = e.detail.value
			},
			JQXChange(e) {
				this.jQXDaoQi = e.detail.value
			},
			RegChange(e) {
				this.regdate = e.detail.value
			},
			hastChange(e){
				
				//var values = e.detail.value; 
				      if(_self.hasT){
						  _self.hasT=false;
						  _self.TL='L'
				        }else{
							_self.TL='T'
				             _self.hasT=true;
				        }
					console.log(_self.hasT);
				  
			},
			ChooseImage() {
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除图片',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							console.log(this.imgList);
							this.imgList.splice(e.currentTarget.dataset.index, 1)
							console.log(this.imgList);
						}
					}
				})
			}
			,SubmitForm(){
				if(this.brandName==''){
					uni.showToast({title:"请选择品牌"});
					return;
				}
				if(this.regdate=='请选择'){
					uni.showToast({title:"请选择上户日期"});
					return;
				}
				
				
				if(this.xingHao==""){
					uni.showToast({title:"请填写型号"});
					return;
				} 
				if(this.showPrice==""){
				 	uni.showToast({title:"请填写显示卖价"});
				 	return;
				}
				 if(_self.imgList.length<1){
				  	uni.showToast({title:"请上传图片"});
				  	return;
				 } 
			
				uni.showLoading({
					  title: '上传中'
				});
				_self.imgLis
				  console.log("一共有："+_self.imgList.length);
				    console.log(_self.imgList);
				this.upimg(0); //上传图片和提交数据
				 
			
				
				
			}
			,upimg(i){  
				 
				if(i<_self.imgList.length){
					if(_self.imgList[i].indexOf(service.BaseUrl) ==-1){
						  console.log("本地："+_self.imgList[i]);
						uni.uploadFile({ 
						         url: service.BaseUrl+'api/apiCar/UpLoadImg',   
						         filePath: _self.imgList[i], // filePath  需要上传的文件
						         name: 'file',
						         success(res1) {
									 var imgres= JSON.parse(res1.data);
									 _self.upimgList.push(imgres.Data);
									 console.log("上传图片返回："+imgres.Data);
									// console.log( (( i+1)*100)+" "+_self.imgList.length +"="+_self.percent);
						     	  _self.percent =  (( i+1)*100)/_self.imgList.length ;
								 _self.upimg(i+1);  //异步原因，只能写在这里面
								
								 }
						     }); 
					}
					else{
						 console.log("无须上传："+_self.imgList[i]);
							 _self.upimgList.push(_self.imgList[i]);
							 _self.percent =  (( i+1)*100)/_self.imgList.length ;
							 _self.upimg(i+1);   
					}
									
				}
				else{
					 var user=service.getUsers();
					 console.log("提交的图片");
					 console.log(_self.upimgList)
					uni.request({
						header:{ 'Authorization' : 'Basic '+service.getUsers().Token},
						method: 'POST',
						url: service.BaseUrl+'api/apiCar/EditCar',
						data: {
							id:_self.id,
							xingHao:_self.xingHao,
							engineCC:_self.engineCC,
							sellPrice:_self.sellPrice,
							showPrice:_self.showPrice,
							costPrice:_self.costPrice,
							brandName:_self.brandName,
							speedAssembly:_self.speedAssembly,
							sYxDaoQi:_self.sYxDaoQi,
							jQXDaoQi:_self.jQXDaoQi,
							regdate:_self.regdate,
							Carimgs:_self.upimgList,
							showTxt:_self.showTxt,
							hideTxt:_self.hideTxt,
							hasT:_self.hasT,
							Wkm:_self.Wkm,
							Token:service.getUsers().Token							
						},
						success: res => {
							if (res.data.Code==1) {
								uni.hideLoading();
								uni.showModal({
								    title: '完成',
								    content: '修改成功',
									 showCancel:false,
								    success: (res) => {
								        if (res.confirm) { 
								                uni.reLaunch({url:"../index/index"}); 
								        } 
								    }});
							}else{
								uni.showToast({
									title:res.data.Msg
								})
							}
						}
					});
					
				}
				   
			}
			
			
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
	
</style>
